Install Docker
curl -fsSL get.docker.com | sh
To use docker as non-root user add user to docker group:
sudo usermod -aG docker $(whoami)
--publish
is specified then engine opens port on a host and forward to port in containerdocker container top container_name
- process list in one containerdocker container inspect container_name
- details of one container config. Shows how container was run.docker container stats container_name
- performance stats for all containers.docker container port container_name
To run a container use:
docker container run [options] image [command] [args…]
-i
flag tells docker to connect to STDIN in the container
-t
flag specifies to get a pseudo-terminal (to run terminal we need to put command /bin/bash
)
docker run -it image bash
-d
flag tells docker to run container in the background (detached mode) or as a daemon
-w
set the current working directory (inside container) for conatiner process
To exit container shell without the container shut down user CTRL+P+Q
Containers can be specified using their ID or name
docker container ls
- list all running containersdocker container ls -a
- list all containers (includes containers that are stopped)To start stopped container use
docker container start <container ID>
To stop container use
docker container stop <container ID>
To remove all containers:
docker container rm $(docker ps -a -q)
docker container exec
- allows to execute command in a running container
docker exec container_id some_node_script.js
To get terminal access to the container use
docker container exec -i -t container_id bash
--publish
docker network ls
- show networksdocker network inspect
- inspect a networkdocker network create --driver
- create a networkdocker network connect
- attach a network to containerdocker network disconnect
- deatach a network from container docker container run --net-alias nginx-alias nginx
To show all available images
docker image ls
To remove image use
docker image rm [-f] <image_id or image_name:tag>
Get the info about image layers
docker image history <image_id or name>
Docker commit - saves changes in a container as a new image
docker commit [options] [container ID] [repository:tag]
Can reference the container with container name instead of ID
docker commit 9283498234c5 max/myapplication:1.0
Dockerfile - is a configuration file that contains instructions for building a Docker image
Best Practise: Each run instruction creates a new file layer in docker container and after each run there is a commit to container. If you want to avoid this multilayer behaviour use &&
syntax
RUN apt-get update && apt-get install -y curl vim openjdk-7-jdk
To build a docker container use:
Syntax:
docker build [options] path
Common option to tag the build
docker build -t [repository:tag] path
path - build context. All other paths are relative to this one. Should contain Dockerfile.
Dockerfile - is a configuration file that contains instrutions for building a Docker image
FROM - instruction specifies what the base image should be
RUN - instruction specifies a command to execute
CMD - defines a default command to execute when a container is created.
Performs no action during the image build
Can only be specified once in a Dockerfile
Can be overridden at run time
You can use Shell and EXEC form
CMD ping 127.0.0.1 -c 30
- shell format
CMD [“ping”, “127.0.0.1”, “-c”, “30”]
- exec format
We can not overide this command at runtime
Run time arguments and CMD instruction are passed as parametrs to the ENTRYPOINT instruction
Shell and EXEC form
EXEC form preffered as shell form cannot accept arguments at run time
ENTRYPOINT [“ping”]
docker run <container> 127.0.0.1 -c 30
Container essentially runs as an executable
MAINTAINER - declare the container maintainer
COPY - copy some files into the container
WORKDIR - defines the currently working directory inside a container. If it doesn’t exit then follder will be created.
At first we need to login to our docker account
docker login
You image should have the name in the following template:
<docker_user_name>/<image_name>:<tag>
To create a new image tag from existed image use
docker tag <old_tag> <new_tag>
To push image to docker hub use:
docker push <username>/<image_name>:<tag>
List all volumes
docker volume ls
VOLUME
command in Dockerfile then when container starts the docker engine creates a volume on a host and mount it to a volume directory in a container.
/var/lib/mysql
$ docker image inspect --format='' mysql:5.6
/var/lib/mysql
Run a new container and mount the folder /container-volume inside it’s file system to some directory in host
docker run -v /container-volume nginx:1.7
Run a new container and map the /data/src folder from the host into /test/src folder in the container
docker run -v /data/src:/test/src nginx:1.7
Run new container and map directory /var/lib/mysql
to some named volume
docker run -v mysql-volume:/var/lib/mysql
VOLUME /myvol
VOLUME /firstfolder /secondfolder
VOLUME [“myvol”, “myvol2”]
To fully remove container with volume run
docker rm -v container_id
docker-compose build
- build or rebuild all services defined in docker-compose.ymldocker-compose build mongo
- build or rebuild mongo servicedocker-compose up
- creates and starts all servicesdocker-comose up --no-deps node
- do not recreate services than node depends ondocker-compose down
- stops and removes all containersdocker-compose down --rmi all --volumes
- remove containers, images, volumesdocker-compose start
- starts all servicesdocker-compose stop
- stops all containersdocker-compose rm
- removes stopped containersdocker-compose logs
- get services logsYou can pass multile files to docker-compose
. In this case every next file will override corresponding settings in previous.
docker-compose -f docker-compose.yml -f docker-compose.admin.yml run backup_db