Common Commands in Docker

Pull a image from Docker Hub

$ sudo docker pull fhsinchy/hello-dock

Run a container:

$ sudo docker container run -d -p 8080:80 fhsinchy/hello-dock
  • -p  --publish it meant any request sent to port 8080 of your host system will be forwarded to port 80 inside the container.
  • -d  --detach  keep a container running in background.

List containers:

$ sudo docker container ls         # List containers that are running.
$ sudo docker container ls --all   # List all containers include those are Exited.
 

How to name or rename a container: 

$ sudo docker container run --detach --publish 8888:80 --name hello-dock-container fhsinchy/hello-dock
$ sudo docker container rename gifted_sammet hello-dock-container-2

How to stop or kill a running container:

$ sudo docker container stop <container identifier>
Where container identifier can either be the id or the name of the container.
$ sudo docker container stop hell-dock-container

How to restart a container:

$ sudo docker container start <container identifier>

How to remove dangling containers:

$ sudo docker container rm <container identifier> 
$ sudo docker container prune   # Remove all dangling containers

How to run a container in interactive mode:

$ sudo docker container run  -it ubuntu
  • -i  Connecting you to the input stream of the container.
  • -t  Make sure that you get some good formatting and a native terminal-like experience by allocating a pseudo-tty.
 

How to  create a docker image:

Take the official nginx image, for example.
$ sudo docker container run --rm -d --name default-nginx -p 8080:80 nginx
  • --rm  Clean up. If you use -d with --rm, the container is removed when it exits or when the daemon exits.
Create a directory named custom-nginx and go inside the custom-nginx directory.
And create a new file name Dockerfile. The content for Dockerfile is as follows:
FROM ubuntu:latest
 
EXPOSE 80
 
RUN apt-get update &&
        apt-get install nginx -y && \
        apt-get clean && rm -rf /var/lib/apt/lists/*
 
CMD ["nginx", "-g", "daemon off;"]

  • Every valid Dockerfile starts with a FROM instruction.
  • The EXPOSE instruction is used to indicate the port that needs to be published.
  • The RUN instruction in a Dockerfile executes a command inside the container shell.
  • Finally the CMD instruction sets the default command for your image. The -g and daemon off are options for nginx. Running nginx as a  single process inside containers is considered a best practice hence the usage of this option.
To build an image using the Dockerfile you just wrote, open up your terminal inside the custom-nginx directory and execute the following command:
$ sudo docker image build . 
  • docker image build is the command for building the image.
  • The  at the end sets the context for this build.
# Sending build context to Docker daemon  3.584kB
# Step 1/4 : FROM ubuntu:latest
#  ---> d70eaf7277ea
# Step 2/4 : EXPOSE 80
#  ---> Running in 9eae86582ec7
# Removing intermediate container 9eae86582ec7
#  ---> 8235bd799a56
# Step 3/4 : RUN apt-get update &&     apt-get install nginx -y &&     apt-get clean && rm -rf /var/lib/apt/lists/*
#  ---> Running in a44725cbb3fa
### LONG INSTALLATION STUFF GOES HERE ###
# Removing intermediate container a44725cbb3fa
#  ---> 3066bd20292d
# Step 4/4 : CMD ["nginx", "-g", "daemon off;"]
#  ---> Running in 4792e4691660
# Removing intermediate container 4792e4691660
#  ---> 3199372aa3fc
# Successfully built 3199372aa3fc
 
Now to run a container using this image:
$ sudo docker container run --rm -d --name custom-nginx-packaged -p 8080:80 3199372aa3fc
 

How to tag docker images:

The --tag or -t option is used in such cases.
 
Generic syntax for the option is as follows:
--tag <image repository>:<image tag>

In  order to tag your custom nginx image with custom-nginx:packaged you can execute the following command:
$ sudo docker image build --tag custom-nginx:packaged . 

In cases where you forgot to tag an image during build time, or maybe you want to change the tag, you can use the image tag command to do that:
$ sudo docker image tag <image id> <image repository>:<image tag>
or
$ sudo docker image tag <image repository>:<image tag> <new image repository>:<new image tag>

How to List and remove docker images:

List images:
$ sudo docker image ls

Remove one or more images:
$ sudo docker image rm <image identifier>
For example remove the image that you taged:
$ sudo docker image rm custom-nginx:packaged 

Remove all untagged dangling images as follows:
$ sudo docker image prune  --force 
 
Remove all cached images in your local registry:
$ sudo docker image prune  --all

How to build nginx from source:

Download nginx-1.20.2.tar.gz
Update Dockerfile's content as follows:
 
FROM ubuntu:latest
EXPOSE 80
RUN apt-get update &&\
    apt-get install build-essential\
                    libpcre3 \
                    libpcre3-dev \
                    zlib1g \
                    zlib1g-dev \
                    libssl1.1 \
                    libssl-dev \
                    -y && \
    apt-get clean && rm -rf /var/lib/apt/lists/*
COPY nginx-1.20.2.tar.gz .
RUN tar -xvf nginx-1.20.2.tar.gz && rm nginx-1.20.2.tar.gz
RUN cd nginx-1.20.2 && \
    ./configure \
        --sbin-path=/usr/bin/nginx \
        --conf-path=/etc/nginx/nginx.conf \
        --error-log-path=/var/log/nginx/error.log \
        --http-log-path=/var/log/nginx/access.log \
        --with-pcre \
        --pid-path=/var/run/nginx.pid \
        --with-http_ssl_module && \
     make && make install
RUN rm -rf /nginx-1.20.2
CMD ["nginx", "-g", "daemon off;"]


Now to build an image using this code, execute the following command:
$ sudo docker image build --tag custom-nginx:built .

This code above is alright but there are some places where we can make improvements.
  • You can create an argument using the ARG instruction instead of hard coding the filename like nginx-1.20.2.tar.gz .
  • There is a another instruction like COPY called the ADD instruction which is capable of adding files from the internet.
Open up the Dockerfile file and update its content as follows:
FROM ubuntu:latest

EXPOSE 80

RUN apt-get update &&\
    apt-get install build-essential\
                    libpcre3 \
                    libpcre3-dev \
                    zlib1g \
                    zlib1g-dev \
                    libssl1.1 \
                    libssl-dev \
                    -y && \
    apt-get clean && rm -rf /var/lib/apt/lists/*

ARG FILENAME="nginx-1.20.2"
ARG EXTENSION="tar.gz"

ADD https://nginx.org/download/${FILENAME}.${EXTENSION} .

RUN tar -xvf ${FILENAME}.${EXTENSION} && rm ${FILENAME}.${EXTENSION}

RUN cd ${FILENAME} && \
    ./configure \
        --sbin-path=/usr/bin/nginx \
        --conf-path=/etc/nginx/nginx.conf \
        --error-log-path=/var/log/nginx/error.log \
        --http-log-path=/var/log/nginx/access.log \
        --with-pcre \
        --pid-path=/var/run/nginx.pid \
        --with-http_ssl_module && \
    make && make install

RUN rm -rf /${FILENAME}

CMD ["nginx", "-g", "daemon off;"]

 
Now you should be able to run a container using the custom-nginx:built image:
$ sudo docker container run --rm --detach --name custom-nginx-built --publish 8080:80 custom-nginx:built

How to optimize docker images:

As you can see on line 3, the RUN instruction installs a lot of stuff. 6 packages that we installed, only two are necessary for running nginx they are libpcre3 and zlib1g. So a better idea would be to uninstall the other packages once the build process is done. To do so, update the Dockerfile as follows:
FROM ubuntu:latest

EXPOSE 80

ARG FILENAME="nginx-1.20.2"
ARG EXTENSION="tar.gz"

ADD https://nginx.org/download/${FILENAME}.${EXTENSION} .

RUN apt-get update &&\
    apt-get install build-essential\
                    libpcre3 \
                    libpcre3-dev \
                    zlib1g \
                    zlib1g-dev \
                    libssl1.1 \
                    libssl-dev \
                    -y && \
    tar -xvf ${FILENAME}.${EXTENSION} && rm ${FILENAME}.${EXTENSION} && \
    cd ${FILENAME} && \
    ./configure \
        --sbin-path=/usr/bin/nginx \
        --conf-path=/etc/nginx/nginx.conf \
        --error-log-path=/var/log/nginx/error.log \
        --http-log-path=/var/log/nginx/access.log \
        --with-pcre \
        --pid-path=/var/run/nginx.pid \
        --with-http_ssl_module && \
    make && make install && \
    cd / && rm -rfv /${FILENAME} && \
    apt-get remove build-essential \
                    libpcre3-dev \
                    zlib1g-dev \
                    libssl-dev \
                    -y && \
    apt-get autoremove -y && \
    apt-get clean && rm -rf /var/lib/apt/lists/*

CMD ["nginx", "-g", "daemon off;"]


How to share your docker images online:

$ sudo docker login
$ sudo docker image build --tag your_dockerhub_name/custom-nginx:latest --file Dockerfile .  
$ sudo docker image push your_dockerhub_name/custom-nginx:latest

How to write the development dockerfile:

Comments

Popular posts from this blog

BdsDex: failed to load Boot0001 "UEFI BHYVE SATA DISK BHYVE-OABE-20A5-E582" from PciRoot(0x0)/Pci (0x2, 0x0)/Stat(0x0,0xFFFF,0x0) : Not Found

How To Install Nginx, MySQL and PHP (FEMP) Stack on FreeBSD 13.0

Install samba on FreeBSD(on VMware Workstation) to share files with Window.