Category: Docker

  • Docker detach container

    Docker container stop once the main program it started with stop running. If you start a docker container with console using -ti option, it stop when you disconnect.

    To avoid process getting stopped, you can detach from the console.

    This is done by pressing keys

    CTRL + P
    

    Followed by

    CTRL + Q
    

    You can just press CTRL key, then press “p”, then “q”.

  • docker images

    docker images command list all docker images (blueprints) available on your server.

    root@ok:~# docker images
    REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
    gcr.io/tensorflow/tensorflow   latest              04f0b1131389        9 months ago        1.13GB
    tensorflow/tensorflow          1.1.0               04f0b1131389        9 months ago        1.13GB
    root@ok:~# 
    
  • docker-compose

    To run a project

    docker-compose up -d
    

    Rebuild project and run

    docker-compose up --build
    

    To stop all containers in the project

    docker-compose down
    

    To see list of containers started by docker-compose, run

    docker-compose ps
    

    To get container id for a container started by docer-compose, run

    docker-compose ps -q NAME_OF_SERVICE
    

    Example

    root@ee:/opt/easyengine/services# docker-compose ps
                Name                           Command               State                    Ports                  
    -----------------------------------------------------------------------------------------------------------------
    services_global-db_1            docker-entrypoint.sh mysqld      Up      3306/tcp                                
    services_global-nginx-proxy_1   /app/docker-entrypoint.sh  ...   Up      0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
    services_global-redis_1         docker-entrypoint.sh redis ...   Up      6379/tcp                                
    root@ee:/opt/easyengine/services# docker-compose ps -q global-nginx-proxy
    140191cb36add7be4f993bbff4a0f8a054d0985486831fcc669a80a577988a92
    root@ee:/opt/easyengine/services# 
    

    docker-compose: error while loading shared libraries
    Docker compose start container on boot

  • Docker Delete all images

    Before you can delete a docker image, you need to delete any container that is based on this image. So see how to delete all docker containers, see

    Delete all docker containers

    To list all available docker images, run

    docker images
    

    To just display image ID only, run with -q option

    docker images -q
    

    Now lets pass the result to docker rmi command using xargs

    docker images -q | xargs docker rmi
    

    This will delete all images. You can also use

    docker rmi $(docker images -q)
    

    See docker

  • Delete all docker containers

    We need to use diff commands to to do this. First lets get list of all containers.

    docker ps -a -q
    

    This will list all docker containers available. Before we can delete, we need to stop them.

    To stop all running containers, use

    docker stop $(docker ps -q)
    

    To delete all containers, run

    docker rm $(docker ps -a -q)
    

    Docker delete all containers

  • Backup and Restore Docker Image

    To backup the docker image, run

    docker save -o ~/IMAGE_NAME.tar IMAGE_NAME

    To restore a container, run

    docker load -i ~/IMAGE_NAME.tar

    Backup and Restore a Docker Container

    To backup, run

    docker export -o CONTAINER_NAME.tar CONTAINER_NAME

    To restore, run

    docker import CONTAINER_NAME.tar serverok/CONTAINER_NAME:2.0

    docker

  • Docker exec

    docker exec command allow running commands inside running containers.

    docker exec -ti CONTAINER_ID /bin/bash
    

    See docker

  • Docker Run

    Docker run command create container from IMAGE and run it.

    docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]
    

    Here we run nginx docker image.

    docker run -d -p 80:80 --name  webserver nginx
    

    -d = detach after starting container.

    -p 80:80 = port 80 on host machine is mapped to port 80 inside container.

    Now you will be able to access Nginx server by visiting IP of host machine.

    To stop running container, run

    root@ok-vm:~# docker stop webserver
    webserver
    root@ok-vm:~# 
    

    To start

    docker start webserver
    

    Docker Volumes

    Docker volume can be specified with -v, it allow you to share a folder in host machine with docker container.

    docker run -d -p 80:80 -v /var/www/html:/usr/share/nginx/html --name  webserver nginx
    

    Here folder /var/www/html on host machine will be mounted as /usr/share/nginx/html in the container. This allows you to make the data persistent.

    See Docker

  • Running MySQL inside docker

    On a Cpanel Server with an old version of PHP, one of the applications requires MySQL 5.6 to work. Since the PHP version is no longer supported, updating MySQL using WHM will upgrade PHP to one of the supported versions, which may break existing sites. So I decided to install MySQL inside docker.

    First, create a folder for MySQL docker to store data.

    mkdir /home/mysql-docker-data

    Now run

    docker run --name serverok-mysql --restart=unless-stopped -v /home/mysql-docker-data:/var/lib/mysql -p 3307:3306 -e MYSQL_ROOT_PASSWORD=KqAtPd3BpyKjQ -d mysql:5.6

    For MairaDB 10.1, run

    docker run --name serverok-mysql --restart=unless-stopped -v /home/mysql-docker-data:/var/lib/mysql -p 3307:3306 -e MYSQL_ROOT_PASSWORD=KqAtPd3BpyKjQ -d mariadb:10.5

    Here I used “KqAtPd3BpyKjQ” as the password, replace it with your password.

    -p 3307:3306 -> tell docker to map port 3306 inside the docker container to port 3307 on host machine.

    This will pull the MySQL docker image and start it.

    Once it finished downloading, you can check if it is running with the command

    root@rapidswitch [~]# docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
    24c86d5ce381        mysql:5.6           "docker-entrypoint..."   8 minutes ago       Up 8 minutes        0.0.0.0:3307->3306/tcp   serverok-mysql
    root@rapidswitch [~]# 

    To connect to MySQL use

    mysql -h 127.0.0.1 --port 3307 -u root -pKqAtPd3BpyKjQ

    Auto Start on Boot

    To start the container on boot, you can run

    docker update --restart=unless-stopped serverok-mysql

    Or use /etc/rc.local, this may not work with newer versions of Ubuntu/Debian

    docker start serverok-mysql
  • docker

    If you need to try docker on web without installing on your computer, use https://labs.play-with-docker.com

    Docker Install

    Docker Basics

    Docker Delete Conatiner/Image

    Run Application in Docker Container

    Creating Docker Images

    Docker Tools

    Using Docker

    To install docker, run

    wget -qO- https://get.docker.com/ | sh

    Install docker compose

    curl -L "https://github.com/docker/compose/releases/download/1.23.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    chmod 755 /usr/local/bin/docker-compose

    Check docker is working

    root@forum:~# docker run hello-world
    Unable to find image 'hello-world:latest' locally
    latest: Pulling from library/hello-world
    ca4f61b1923c: Pull complete 
    Digest: sha256:66ef312bbac49c39a89aa9bcc3cb4f3c9e7de3788c944158df3ee0176d32b751
    Status: Downloaded newer image for hello-world:latest
    
    Hello from Docker!
    This message shows that your installation appears to be working correctly.
    
    To generate this message, Docker took the following steps:
     1. The Docker client contacted the Docker daemon.
     2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
        (amd64)
     3. The Docker daemon created a new container from that image which runs the
        executable that produces the output you are currently reading.
     4. The Docker daemon streamed that output to the Docker client, which sent it
        to your terminal.
    
    To try something more ambitious, you can run an Ubuntu container with:
     $ docker run -it ubuntu bash
    
    Share images, automate workflows, and more with a free Docker ID:
     https://cloud.docker.com/
    
    For more examples and ideas, visit:
     https://docs.docker.com/engine/userguide/
    
    root@forum:~# 

    To list all running docker containers

    root@rapidswitch [~]# docker ps
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
    root@rapidswitch [~]#