build: create your own Docker container

Introduction

A frequent task when setting up an environment is creating your own containers.

Basics

First of all, create a file with the Docker settings - Dockerfile and edit it, for example, with the editor vim

touch Dockerfile
vi Dockerfile

FROM debian RUN apt -y update

Now you need to build an image, and for that you need the build command

docker build -t andrei-debian:1.0 .

-t is short for --tag, it sets the name and tag in the 'name:tag' format

the tag can be omitted - it is optional

. means - in the current directory

You can check that this image is in the list with the images command

docker images

REPOSITORY TAG IMAGE ID CREATED SIZE andrei-debian 1.0 034af79c78c2 4 minutes ago 142MB debian latest a11311205db1 27 hours ago 124MB

The downloaded image has appeared debian and the andrei-debian image, just created on its basis.

Now run it with the command run

docker run --name debian1 andrei-debian:1.0

The container will start and immediately shut down, because it has no processes inside

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 97f8baddbe9f andrei-debian:1.0 "bash" 6 seconds ago Exited (0) 5 seconds ago debian1

docker run -it --name debian1 andrei-debian:1.0

The terminal greeting will appear inside the container

Open a new terminal and make sure the container status is Up

docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e901ad90703c andrei-debian:1.0 "bash" 9 seconds ago Up 9 seconds debian1

Specify the Dockerfile explicitly

If a single directory contains several Dockerfiles, you can explicitly specify which of them should be used for the build.

The -f flag is used for this

docker build -t andrei-debian:1.0 -f Dockerfile.debian .

Dockerfiles must be named according to the documentation :

Dockerfile.name

For example, I create my own image based on Ubuntu and with tests on PyTest with the following command

docker build --tag pytest_ubuntu:1.0 -f ./dockerfiles/Dockerfile.ubuntu .

The image will be called pytest_ubuntu and have the tag 1.0

docker images

REPOSITORY TAG IMAGE ID CREATED SIZE pytest_ubuntu 1.0 eb4761442740 About an hour ago 1.79GB

Help

docker build --help

Usage: docker build [OPTIONS] PATH | URL | - Build an image from a Dockerfile Options: --add-host list Add a custom host-to-IP mapping (host:ip) --build-arg list Set build-time variables --cache-from strings Images to consider as cache sources --cgroup-parent string Optional parent cgroup for the container --compress Compress the build context using gzip --cpu-period int Limit the CPU CFS (Completely Fair Scheduler) period --cpu-quota int Limit the CPU CFS (Completely Fair Scheduler) quota -c, --cpu-shares int CPU shares (relative weight) --cpuset-cpus string CPUs in which to allow execution (0-3, 0,1) --cpuset-mems string MEMs in which to allow execution (0-3, 0,1) --disable-content-trust Skip image verification (default true) -f, --file string Name of the Dockerfile (Default is 'PATH/Dockerfile') --force-rm Always remove intermediate containers --iidfile string Write the image ID to the file --isolation string Container isolation technology --label list Set metadata for an image -m, --memory bytes Memory limit --memory-swap bytes Swap limit equal to memory plus swap: '-1' to enable unlimited swap --network string Set the networking mode for the RUN instructions during build (default "default") --no-cache Do not use cache when building the image --pull Always attempt to pull a newer version of the image -q, --quiet Suppress the build output and print image ID on success --rm Remove intermediate containers after a successful build (default true) --security-opt strings Security options --shm-size bytes Size of /dev/shm -t, --tag list Name and optionally a tag in the 'name:tag' format --target string Set the target build stage to build. --ulimit ulimit Ulimit options (default [])

Article author: Andrei Olegovich