Welcome to our comprehensive guide on Docker, the revolutionary containerization platform that has taken the world of software development by storm. If you’re looking to gain an edge in today’s competitive digital landscape, mastering Docker is a must. In this article, we’ll provide you with an in-depth Docker cheat sheet that covers everything you need to know to get started and stay ahead of the competition. So, let’s dive in!
- What is Docker?
- Why Docker is a Game Changer
- Docker Basics: Getting Started
- Docker CLI
- Manage images
- Manage containers
- Also, see
- Advanced features
What is Docker?
Docker is an open-source platform that allows developers to automate applications’ deployment, scaling, and management in lightweight, portable containers. Containers are isolated environments that contain everything needed to run a piece of software, including the code, runtime, system tools, and libraries. Unlike traditional virtualization, which emulates an entire operating system, Docker containers share the host system’s kernel, making them lightweight, fast, and efficient.
Why Docker is a Game Changer
Docker has revolutionized the way software is developed, deployed, and managed. Here are some key reasons why Docker is a game changer in the world of software development:
Portability: Docker containers are self-sufficient and can run on any system that supports Docker, regardless of the underlying infrastructure. This makes it easy to deploy applications across different environments, such as development, testing, and production, without worrying about compatibility issues.
Scalability: Docker enables horizontal scaling, allowing applications to be seamlessly scaled up or down based on demand. This makes it ideal for modern, cloud-native applications that handle variable workloads efficiently.
Consistency: Docker containers encapsulate the entire application stack, including the code, runtime, and dependencies. This ensures that applications run consistently across different environments, eliminating the dreaded “it works on my machine” problem.
Speed: Docker allows for rapid deployment and testing of applications, making it ideal for agile development workflows. Containers can be spun up in seconds, enabling quick iteration and faster time to market.
Security: Docker provides built-in isolation between containers, making it harder for malicious code to escape and compromise the host system. Containers can also be configured with fine-grained security settings, making them more secure than traditional monolithic applications.
Docker Basics: Getting Started
Now that you have a basic understanding of what Docker is and why it’s a game changer, let’s dive into the fundamentals of Docker. Here are the essential Docker commands and concepts you need to know to get started:
docker build [options] . -t "app/container_name" # name
image from a Dockerfile.
docker run [options] IMAGE # see `docker create` for options
Run a command in an
docker create [options] IMAGE -a, --attach # attach stdout/err -i, --interactive # attach stdin (interactive) -t, --tty # pseudo-tty --name NAME # name your image -p, --publish 5000:5000 # port map --expose 5432 # expose a port to linked containers -P, --publish-all # publish all ports --link container:alias # linking -v, --volume `pwd`:/app # mount (absolute paths needed) -e, --env NAME=hello # env vars
$ docker create --name app_redis_1 \ --expose 6379 \ redis:3.0.2
container from an
docker exec [options] CONTAINER COMMAND -d, --detach # run in background -i, --interactive # stdin -t, --tty # interactive
$ docker exec app_web_1 tail logs/development.log $ docker exec -t -i app_web_1 rails c
Run commands in a
docker start [options] CONTAINER -a, --attach # attach stdout/err -i, --interactive # attach stdin docker stop [options] CONTAINER
$ docker ps $ docker ps -a $ docker kill $ID
containers using ps/kill.
$ docker images REPOSITORY TAG ID ubuntu 12.10 b750fe78269d me/myapp latest 7b2431a8d968
$ docker images -a # also show intermediate
docker rmi b750fe78269d
- Getting Started (docker.io)
ENV APP_HOME /myapp RUN mkdir $APP_HOME
RUN bundle install
VOLUME ["/data"] # Specification for mount point
ADD file.xyz /file.xyz COPY --chown=user:group host_file.xyz /path/container_file.xyz
ONBUILD RUN bundle install # when used with another file
EXPOSE 5900 CMD ["bundle", "exec", "rails", "server"]
ENTRYPOINT ["executable", "param1", "param2"] ENTRYPOINT command param1 param2
Configures a container that will run as an executable.
ENTRYPOINT exec top -b
This will use shell processing to substitute shell variables and will ignore any
docker run command line arguments.
LABEL "com.example.vendor"="ACME Incorporated" LABEL com.example.label-with-value="foo"
LABEL description="This text illustrates \ that label-values can span multiple lines."
# docker-compose.yml version: '2' services: web: build: . # build from Dockerfile context: ./Path dockerfile: Dockerfile ports: - "5000:5000" volumes: - .:/code redis: image: redis
docker-compose start docker-compose stop
docker-compose pause docker-compose unpause
docker-compose ps docker-compose up docker-compose down
web: # build from Dockerfile build: .
# build from custom Dockerfile build: context: ./dir dockerfile: Dockerfile.dev
# build from image image: ubuntu image: ubuntu:14.04 image: tutum/influxdb image: example-registry:4000/postgresql image: a4bc65fd
ports: - "3000" - "8000:80" # guest:host
# expose ports to linked services (not to host) expose: ["3000"]
# command to execute command: bundle exec thin -p 3000 command: [bundle, exec, thin, -p, 3000]
# override the entrypoint entrypoint: /app/start.sh entrypoint: [php, -d, vendor/bin/phpunit]
# environment vars environment: RACK_ENV: development environment: - RACK_ENV=development
# environment vars from file env_file: .env env_file: [.env, .development.env]
# makes the `db` service available as the hostname `database` # (implies depends_on) links: - db:database - redis
# make sure `db` is alive before starting depends_on: - db
# make this service extend another extends: file: common.yml # optional service: webapp
volumes: - /var/lib/mysql - ./_data:/var/lib/mysql
services: web: labels: com.example.description: "Accounting web app"
services: web: dns: 18.104.22.168 dns: - 22.214.171.124 - 126.96.36.199
services: web: devices: - "/dev/ttyUSB0:/dev/ttyUSB0"
services: web: external_links: - redis_1 - project_db_1:mysql
services: web: extra_hosts: - "somehost:192.168.1.100"
To view the list of all the services running in the swarm
docker service ls
To see all running services
docker stack services stack_name
to see all services logs
docker service logs stack_name service_name
To scale services quickly across qualified node
docker service scale stack_name_service_name=replicas
To clean or prune unused (dangling) images
docker image prune
To remove all images which are not in use containers, add – a
docker image prune -a
To prune your entire system
docker system prune
To leave swarm
docker swarm leave
To remove swarm ( deletes all volume data and database info)
docker stack rm stack_name
To kill all running containers
docker kill $(docekr ps -q )