A guide for Docker along with examples of Dockerfiles and Docker-compose files.
docker container ls
docker ps
Flags:
-a = show all containers -q = show all active container ids -ql = show last active container id -qa = all container ids docker start $(docker ps -ql) docker ps -qa | xargs docker stop
docker inspect container_id || container_name
docker start container_id || container_name
docker stop container_id || container_name
docker rm container_id || container_name
Delete all:
docker ps -qa | xargs docker rm -f
docker run base_image_name:tag_name + args_for_ENTRYPOINT --name name_container
- If no local image found, runs Docker pull to check Docker Hub
-it = shell into the running container
-e = environment variable -e PORT...
--rm removes container after shutdown
docker rename container_id || old_container_name new_container_name
docker rm container_id || container_name
docker pull username/image_name:tag_name
- Default tag name is latest
docker images
docker name_of_image:tag_name history
Dockerfile:
FROM base_image_name:tag_name RUN install x && install y RUN install y COPY file_name + destination WORKDIR starting_directory_for_CMD CMD npm start ENTRYPOINT ["command", "command", "command"] MAINTAINER author_name + author_email EXPOSE port_number ENV environment_var USER command_line_user
-
RUN installs, commands end after execution always put before COPY src
-
CMD only executed once per Dockerfile as default command
-
ENTRYPOINT allows for args to be passed in via terminal with docker run to be executed by ENTRYPOINT overriding a potentially a default CMD
-
COPY copies directories recursively. All files in the root directory can be selected with . The root directory of the build starts with / for the destination. best practice is . /src/
-
VOLUME directory to hold persistent data
-
WORKDIR sets directory for run commands, instead of cd directory_name
-
ENV can be injected on docker run with -e
-
USER default is root user
docker build -t name_of_image:tag_name + Dockerfile_location
Flags:
-q with build = limits output --no-cache = build from scratch
docker push username/image_name:tag_name
docker rmi container_id || container_name
-In order to connect to a container you must publish the exposed ports
-
You can do this with the -P flag during container creation then run docker ps to find the port mapping
-
Or you can manually map the port to connect with it using -p exposed_port:container_port
-
bridge network (same computer) is the default
-
--net none is null network driver
-
overlay network (multiple computers)
-
can use --net to connect new container to existing network
-
can use same container_name if different networks using --net-alias
docker network ls
docker run -v host_location:container_location
- docker-compose finds all child Dockerfiles to run multiple containers in default bridge network
docker-compose.yaml:
service_name: build: src_directory image: ports: networks: volumes: environment: command: -build can be written as build: context: ../../jenkins/docker
version: "3" services: postgres: container_name: class_postgres_4 image: postgres:11.1 restart: unless-stopped networks: - my-net ports: - "15432:5432" environment: POSTGRES_USER: "postgres" POSTGRES_PASSWORD: "${MY_PG_PASS}" POSTGRES_DB: "gogs" healthcheck: test: ["CMD", "pg_isready", "-U", "postgres", "-d", "gogs"] interval: 10s timeout: 5s retries: 5 Defining the network via docker-compose: networks: my-net: driver: bridge
docker network ls
docker network create network_name
docker run --net network_name
docker-compose ps
docker-compose up -d
docker-compose down
docker-compose kill
docker-compose rm
docker-compose build
docker volume ls
- New Relic’s Centurion
- Spotify’s Helios
- Ansible’s Docker
- Google’s Kubernetes
- Apache Mesos
- Amazon EC2 Container Service
- Google Container Engine
- Red Hat OpenShift 3
- Joyent Triton
- Microsoft Azure