In this article we will be discussing different ways to run docker container. I would recommend you to go through this installation steps before moving ahead.
1. Run docker container and run a command within it.
root@ubuntu:~# docker run -it ubuntu latest: Pulling from library/ubuntu 6b98dfc16071: Pull complete 4001a1209541: Pull complete 6319fc68c576: Pull complete b24603670dc3: Pull complete 97f170c87c6f: Pull complete Digest: sha256:5f4bdc3467537cbbe563e80db2c3ec95d548a9145d64453b06939c4592d67b6d Status: Downloaded newer image for ubuntu:latest root@5bfa20a5e6b6:/# id uid=0(root) gid=0(root) groups=0(root) root@5bfa20a5e6b6:/# hostname 5bfa20a5e6b6 root@5bfa20a5e6b6:/# root@5bfa20a5e6b6:/# w 14:20:20 up 23 min, 0 users, load average: 0.20, 0.05, 0.02 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root@5bfa20a5e6b6:/#
In the above example docker pulls the latest Ubuntu image and run’s as container. Here we are using option “-it” which are mean for interactive and termincal/pesudo TTY, because of which are are able to run “/bin/bash” command on the container. Kindly note that, once you logged out of the container it gets automatically stopped, see the below results from docker ps -a command.
root@ubuntu:~# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5bfa20a5e6b6 ubuntu "/bin/bash" 5 minutes ago Exited (0) 4 seconds ago loving_mcnulty root@ubuntu:~#
In docker with “docker ps” command you can able to list only container|s which are in running state, so in order to list all container including stopped container we need to use “docker ps -a” option.
2. Run docker container with name:
In the above example you can see the some random name given to the container “loving_mcnulty”. In case you want to give name of your choice follow below command:
root@ubuntu:~# docker run -it --name myubuntu ubuntu
3. Run docker container with publishing port:
In case you are starting container which depends on any specific port then you need to publish containers port to the host. Lets say you want to run apache (htttpd) container which listens on port 80 and you want to publish it with port 80 of host. Then use below command:
root@ubuntu:~# docker run -d --name webserver -p 80:80 httpd 9543303da7af36f318836cdbd80f68535af03a7bb621a536ee84b19fedce1bdd root@ubuntu:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9543303da7af httpd "httpd-foreground" 12 seconds ago Up 11 seconds 0.0.0.0:80->80/tcp webserver root@ubuntu:~#
In the above command we have used “-d” option which makes the container to run in detached mode which makes the container remains to run in the background.
4. Run docker container with remove on exit:
In case you want to remove the container while exiting the container use “–rm” flag:
# docker run --rm --name myserver ubuntu
5. Run docker container with mount point from host:
In case you want to local host mount point or directory on the container use option “-v”.
root@ubuntu:~# mkdir /opt/app root@ubuntu:~# docker run --name web -d -v /opt/app:/app httpd
In the second command we mounted local /opt/app directory to /app on the container.
In case you want to login to container use “docker exec” command with following options:
root@ubuntu:~# docker exec -it web /bin/bash root@7530561dfe4f:/usr/local/apache2# df -h Filesystem Size Used Avail Use% Mounted on overlay 7.7G 1.5G 6.3G 20% / tmpfs 64M 0 64M 0% /dev tmpfs 496M 0 496M 0% /sys/fs/cgroup /dev/xvda1 7.7G 1.5G 6.3G 20% /app shm 64M 0 64M 0% /dev/shm tmpfs 496M 0 496M 0% /proc/scsi tmpfs 496M 0 496M 0% /sys/firmware root@7530561dfe4f:/usr/local/apache2# cd /app
Here in this output you can see /app mount point mounted from local /opt/app directory.
Leave a Reply