Docker Commands

Docker Images

This command show a list of the images that you had in your drive.

docker images

Docker RUN

This command combine the docker create and docker start, if no images found on locally, it will search from remote repository.

Arguments:

  • -d deamon
  • -p port
  • -v mount drive
  • –name assign container name
  • -e environment variable
  • -it interactive terminal mode
  • -t tag
  • –link link to another container eg database:mysql
  • -c command
  • –rm terminate, remove when exit. See example 2
docker run [image]

Docker Run In Deamon

Run in local deamon mode, expose port from container back to local and m ount from container folder to local drive. This example is useful when you’re simulating the environment but the source code still remain in your local drive.

docker run -d -p 5000:5000 -v /User/workspace/tutorial:/var/www/html/tutorial --name web tutorial

Example 2

This example show to spin up an container when exits it will terminated.

First we check the container that running with docker ps command.

$ docker ps 
4b5e6d1514c8 mariadb:latest "/docker-entrypoint. 2 weeks ago Up 5 days 0.0.0.0:3306->3306/tcp db

Now we want to connect with the follow command.
–name mysqlclient  = assign a name to that container
–it = interactive terminal
–link db:mysql = link this container to db:mysql container
–rm = terminate when exists
mysql = existing image name

docker run --name mysqlclient -it --link db:mysql --rm mariadb sh -c 'exec mysql -uroot -p123 -hmysql'

Docker Link [Option -a]

Docker Interactive

This command show how to start a container with -it option interact with the container.

docker-interactive

Docker PS [Option -a]

This command show the current running containers, with optional parameter -a show containers which is stop/exits.

docker ps --OR-- docker ps -a

Docker Build

This command build the images from your local drive.
PS: dot means current folder with Dockerfile
**Note: You will need to have Dockerfile

docker build -t [image-name] .

Docker Info

docker info

Docker RM

This command delete a containers

Docker RMI [hash]

Docker RMI

This command delete a images.
**Note: You cannot delete an images with any containers was instantiated base on this image, regardless any of the container status (running/stop). You need to delete the container first.

docker rmi [hash]

Docker delete all containers

docker rm $(docker stop $(docker ps -a -q) )

Docker LOGS

This command show the activities that logs in the container

docker logs -f [container-name]

Access Container

This command access to docker container. exec – execute, it – interactive terminal

docker exec -it [name] bash

Container IP Address – Boot2Docker & Docker Machine

This command show the container IP address, by default it will not changed. (OLD)

boot2docker ip

docker-machine ip

UPDATE:  In order to obtain the docker information or meta data, we can use docker inspect command, it will return a json data of the containers.

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' CONTAINER_ID

Docker Export Port

This command show the container expose port 80 to host machines port 8000. It host access to container.

$docker run -p 8000:80 nginx

Docker Export Dynamic Port

This command show the container expose port 80 to host machines dynamic port number. It host access to container.

$docker run -P nginx

Docker Port Check

This command check the container port expose to host

$docker port [container-id]

80/tcp -> 0.0.0.0:8000

Copy file from Container to Host

This command show how to copy files from containers to host’s current directory.

$docker cp [name]:/usr/share/nginx/html .

Docker Mount Host Volume

This command show how to mount the tomcat log folder to host log folder.

docker run -d -p 8080:8080 -v $(pwd)/log:/usr/shared/tomcat/log --name tomcat

Docker Create Shared Volume

docker create -v [volume_path]

Docker Networking List

$ docker network ls

NETWORK ID          NAME                DRIVER              SCOPE
e06083df6126        bridge              bridge              local               
0a10fd9ae34c        host                host                local               
6feb992c12b2        none                null                local

Bridge network
Container run on bridge network cannot be accessed outside the docker host, unless it map to docker host port with -p (-P) fix/random port.

docker0 is a bridge network, to identified it use the following command.

docker-machine ssh default ifconfig

docker0   Link encap:Ethernet  HWaddr 02:42:7E:A7:3C:97  
          inet addr:172.17.0.1  Bcast:0.0.0.0  Mask:255.255.0.0
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

None Network

None network is nothing, any docker container run on this network is totally isolate from itself.

Host network

Container run on host network shared the exactly same network setting.

User Define Network – Bridge type

The drawback will be not accessible with other default bridge network container. –link function is not possible

docker network create --driver bridge [name]

docker network create --driver bridge mynetwork

User Define Network – Alias

docker run -d --net mynetwork --name myapp --net-alias safe-alias nginx

-- To connect --
docker run --net mynetwork [command] [image] [dns-alias]:[port]

Docker Assign Network

docker run -d --net [none/custom]

Docker delete all  containers

docker rm $(docker ps -a -q)

Docker delete all images

docker rmi $(docker images -q)

Docker Commands

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.