$ docker ps
$ docker ps -a
docker inspect ubuntu
$ docker run nginx
Press Ctrl + C to stop a container in attached mode.
$ docker run -d nginx
$ docker stop <CONTAINER ID>
Stop the container if running, then remove the container.
$ docker stop <CONTAINER ID>
$ docker rm <CONTAINER ID>
To remove multiple containers
$ docker rm <CONTAINER ID> <CONTAINER ID> <CONTAINER ID>
To remove containers forcefully
$ docker rm -f <CONTAINER ID>
$ docker images
$ docker rmi <IMAGE ID>
To run a container with port 38282 of the host system pointing to 8080 of the container.
docker run -p 38282:8080 kodekloud/simple-webapp:blue
$ docker build -t <<tag-name>> /path/to/Dockerfile
docker run <IMAGE NAME>:<TAG NAME> cat /etc/*release*
$ docker run python:3.6 cat /etc/*release*
CMD sleep 10
or
CMD ["sleep", "10"]
...
ENTRYPOINT["sleep"]
Now we can run docker run ubuntu-sleep 10 and 10 will be appended in the dockerfile like sleep 10
...
ENTRYPOINT["sleep"]
CMD["5"]
Now we if we run docker run ubuntu-sleep without any argument, then the argument in the CMD will appended to ENTRYPOINT and sleep 5 will be executed
Docker has three types of networks
- Bridge
- None
- Host
In bridge network is a private network created by docker. Its usally start with the IP address 172.17.X.X series. Its the default network used by the docker. Example:
docker run ubuntu
None networker isolate the docker contiainer and wouldn't be accessable to external network. Example:
docker run ubuntu --network=none
In Host network, the port of the container is attached to the port of the host. Non network isolation is Example:
docker run ubuntu --network=host
docker network create \
--driver bridge \
--subnet 182.18.0.0/16 ubuntuNOTE: The data stored in the container is not permanent. When the container goes down, the data in it will be erased. To presist data use volumes.
# Old method
docker run -v data_volume:/var/lib/mysql mysql
# New method
docker run --mount type=volume,source=data_volume,target=/var/lib/mysql mysqlThis command will create a new folder named data_volume in the /var/lib/docker/volumes/ directory in the host system and sync the data in container path /var/lib/mysql.
# Old method
docker run -v /data/mysql:/var/lib/mysql mysql
# New method
docker run --mount type=bind,source=/data/mysql,target=/var/lib/mysql mysqlThis command will bind the existing host directory /data/mysql to the container path /var/lib/mysql.
Compose is a tool for defining and running multi-container Docker applications. Its uses a yaml file for configuring the services
Example:
The following docker run commands can be made into a single yml file and running a command like docker-compose up will start the containers.
# In-memory storage
docker run -d --name=redis redis
# Database
docker run -d --name=postgres:9.4
# Backend
docker run -d --name=vote -p 5000:80 --link redis:redis voting-app
# Frontend
docker run -d --name=result -p 5001:80 --link db:db result-app
# Worker
docker run -d --name=worker --link db:db --link redis:redis worker# docker-compose.yml
version: 3
services:
redis:
image: redis
db:
image: postgres:9.4
vote:
image: voting-app
ports:
- 5000:80
links:
- redis:redis
result:
image: result-app
ports:
- 5000:80
links:
- db:db
worker:
image: worker
links:
- redis:redis
- db:db