Docker
Table of Contents
Introduction
- Crash course
- Docker in 100 secs
- Aim: To run software services/progs/applications inside containers.
- Eg: Apache, MySQL, MongoDB, NginX (web server common to Apache), Wordpress, etc…
- Advantages: Convenient, fast, simple, easy to re-create the same environment.
- Docker Hub == GitHub of Docker
NOTE:
- Docker Desktop for Windows Home is here!
- For Win 10 Home (2004 and above), Pro and Enterprise, install Docker Desktop.
- For Win 10 Home 1909 and lower, install Docker Toolkit.
- Docker might not work on VSCode integrated terminal. In that case, open VSCode as an administrator, use ext cmd or use powershell.
- Some commands might not work in cmd. Use Powershell in that case.
$(pwd)
in MacOS is$(%cd%)
in Cmd and${PWD}
in Powershelllocalhost:port
might not work. Use198.168.99.100:port
instead.- Container privileges
Linux and Networking
Please refer to linux-networking.md.
Commands
Miscellaneous Commands
docker version
- Server: Docker Engine (built on Go lang)
docker info
Images
- To view images on local machine:
docker images
- To remove images:
docker image rm ed2
(the entire image ID is not required) - To pull image from DockerHub:
docker pull nginx
- To build image
docker image build -t harshkapadia/nginx-website .
harshkapadia/nginx-website
= Docker_ID/img_name.
= refering to Dockerfile in currect dir (make sure your cmd is pointing to the correct folder)
- To push image to DockerHub:
docker push harshkapadia/nginx-website
Containers
- To show running containers
docker container ls
docker container ps
docker ps
- To show all containers on the system
docker container ls -a
docker ps -a
- To run container in the foreground
docker container run -it -p 80:80 --name my_custom_name_for_nginx nginx
container
= management commandrun
= sub-commandit
= interactive terminal, ie, run in the foreground-p
or--publish
= publish80:80
= mapping ports (portto_be_used_on_local_machine(useany_port):port_exposed_from_container- (use_default_port_of_service_in_container))nginx
= image name
- Once the img is downloaded:
localhost:80
orlocalhost
(default is 80) or127.0.0.1
(localhost)- Win 10 Home users (might not be needed with Docker Desktop):
192.168.99.100
(Since Toolbox is running a Linux VM in VirtualBox, localhost won’t work. Use192.168.99.100
(default) instead.)
- To stop container: ctrl + c
- To run container in the background:
docker container run -d -p 8080:80 --name my_custom_name_for_nginx nginx
-d
or--detach
= detached
- To stop container:
docker stop my_custom_name_for_nginx
ordocker container stop my_custom_name_for_nginx
- To view:
192.168.99.100:8080
(Win 10 Home users - might not be needed with Docker Desktop -localhost
might work)
- To remove container:
docker container rm c699
(the entire container ID is not required) (deleting container does not remove the image from the system) - To remove running container:
- First stop the container and use the remove command
- OR use force:
docker container rm c699 -f
- To remove all containers on the system:
docker rm $(docker ps -aq) -f
(-f
added to remove any running container(s) as well) (this ran in powershell, but not in cmd) - Env variables:
docker container run -d -p 3306:3306 --name my_sql_user --env MYSQL_ROOT_PASSWORD=123456 mysql
(you can find the env vars on Docker Hub) - To start a new container from an existing image:
docker container run -d -p 80:80 --name custom_name_nginx nginx
- To start an existing container:
docker start <container_id>
- To enter a container:
docker container exec -it mynginx bash
(start the container using the abv cmd before doing this)- After entering:
- To view file system:
ls
- To get to index.html (displayed on screen at port) (for nginx):
cd usr/share/nginx/html
- To come out of container:
exit
- To edit file, refer to the bind mount section below.
- To view file system:
- After entering:
Bind Mount
NOTE: Ports 80 or 8081 might not work with below example. Use 8080, 8082, 8083…
- To create container:
- With Docker Toolbox:
- Refer to
bind_mount.md
for further details on ‘x_drive’. - Make sure docker-machine is running (
docker-machine start
) docker container run -d -p 8080:80 -v /x_drive/coding/docker/docker_basics/mounting_example:/usr/share/nginx/html --name - nginx-website nginx
- Refer to files in the
mounting_example
folder.
- Refer to
- With Docker Desktop:
docker container run -it -v "%cd%":/mnt --name kali kalilinux/kali-rolling
- With Docker Toolbox: