Friday, June 26, 2020

Common Docker, Dockerfile and Bash commands

Docker commands (in bash)

docker images
list all images available on local

docker rmi imageNameOrID
docker rmi imagesNameOrID -f 
remove a docker image from local
using -f to force remove image even if it is attached to some containers 

docker build -t imagename .
docker build-t imagename:version .
build a docker image and give it a specified name using the dockerfile in the current directory.
-t to tag the image to a particular name and version

docker ps -a -q
docker container ls
list all running containers, -a (all) to show all container, by default, only running container shows. -q only show container ID.
 

docker ps -a
docker container ls -a
list all running and stopped containers

docker kill containername
kill a running container by name

docker rm containername -f
remove a container (using -f option to force to remove running container)

docker run --name ContainerName -d -p 8000:80 Imagename
run a nginx image of imagename in detached mode (-d), with host port (-p)8000 maps to nginx container's default port 80. By default, the nginx listens on port 80 after starting the container.  Assuming the index.html is in the host's current folder, then it can be loaded from browser with the below link

docker run --rm -it imagename bash
--rm :automatically remove container when it exits
-it bash: interactively start a bash command console, start the container and also open a bash command console, 

docker start containername
docker stop containername
docker restart containername
start, stop, restart a container by name

docker attach containername
attach docker container to std to see the container's console output, using ctrl+c to exit.

docker exec -it containername /bin/bash
docker exec containername bashcommand
attach to container's bash console to run bash command inside container, using exit command to exit.
Once enter the bash command console, you can install other required software, for example, installing vim as below:
apt-get update
apt-get install vim
using below exec command to show all environment variables
docker exec containername env

docker cp container:SrcPath hostDestPath
docker cp hostSrcPath container:destPath
copy files between container and host


docker stop $(docker ps -aq)
stop all containers

docker rm $(docker ps -aq)
delete all containers

docker rmi $(docker images -q)
delete all docker images

Steps to push local docker image to docker hub
1. First login to docker.com using your docker id and password
docker login

2. Tag the local image with the namespace of your docker userid
docker image tag myimage:mytag myDockerUserID/myimage:mytag

3. push the tagged image to docker hub
docker image push myDockerUserID/myimage:mytag

DockerFile commands:

FROM nginx
FROM node:
download the base image from dockerhub or local

COPY . /usr/share/nginx/html
copy the static files from the current directory to the specified folder in docker container. The above sample copies files to container's /usr/share/nginx/html folder. 

ADD https://www.python.org/ftp/python/3.5.1/python-3.5.1.exe /temp/python-3.5.1.exe
copy files from source folder or external url to the destination folder in the container

WORKDIR directoryName
change the current directory in container

RUN shellCommand
run a shell command on the top of current generated image and commit to the updated image, for example,
RUN echo 'run docker file

ARG varnameWithoutDefaultValue
ARG varname=varDefaultValue
Set a variable that can be set by docker build, those variables are only used during docker build, not available when running the container.
Those variable can be set or replaced when running docker build with --build-arg flag
Example - the below code logs the variable DIST_DIR
FROM nginx:1.18
ARG DIST_DIR=dist
RUN echo 'DIST_DIR is set to' ${DIST_DIR}

ENV envname=envValue
ENV envName the environment value
Set environment variable for docker run, those variables are used during docker build and docker run. The ENV value can be replaced using docker run --env flag.
ENV USER_ID=12345 C
RUN echo 'User ID is set to' ${USER_ID}

ENTRYPOINT ["ExeBashName"]
CMD ["p1", "p2"]
Specify the executable name by EntryPoint, as well as default parameters by CMD. Unlike, Run, CMD does not execute anything at build time, but specifies the intended command when starting the image.



Nginx docker image notes
The default docker config file for nginx is in etc/nginx/config.d/default.conf
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

 
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }

Command and Argument for docker

CMD
CMD ["nginx"] // example in nginx docker file
CMD ["bash"] //example in ubuntu docker file
specify the command that will run when docker container starts, CMD configuration can be overridden by docker run parameter
docker run ubuntu sleep 5
sleep 5 will replace default "bash" command specified in docker file. You can also create a new docker file to replace the CMD

CMD["sleep", "5"] //the first element in the array is command to execute. This command can be overridden by
docker run newubuntuimage sleep 10

If you only want to specify the command to execute, and leave argument to be overridden, then use ENTRYPOINT in docker file
ENTRYPOINT ["sleep"]
in that case, any docker run command line argument will be appended after the entrypoint command
docker run ubuntu-sleeper 100
will run sleep 100 in docker image

so, CMD will be replaced by command line argument, while ENTRYPOINT will be appended with command line argument.

When both CMD and ENTRYPOINT are used, then entrypoint command is the execution command, and the default argument is what specified in
CMD, CMD value can be overridden by the docker run's argument if provided.
ENTRYPOINT ["sleep"]
CMD ["5"]







No comments:

Post a Comment