Skip to content

Instantly share code, notes, and snippets.

@tcd1558
Last active August 31, 2021 18:06
Show Gist options
  • Save tcd1558/dcbe96b474edb92bb3be037640d6ca92 to your computer and use it in GitHub Desktop.
Save tcd1558/dcbe96b474edb92bb3be037640d6ca92 to your computer and use it in GitHub Desktop.
Docker: Learn how to use a Dockerfile

Docker: Learn how to use a Dockerfile

0. Overview

With the help of a Dockerfile, you can create a docker container image. To make things easy, you can build your docker image step by step.

1. Index

  1. Overview
  2. Index
  3. Syntax
  4. First step
  5. Build your image

2. Syntax

[button] - is a button on the screen with the text 'button' on it.   
InputField: - is a field labeled 'InputField:', where you can enter text.   
<PlaceHolder> - a placeholder to be replaced with your own text (without the '<' and '>').  
[v] Label1 - ensure there is a check mark in front of the label 'Label1'
[x] Label2 - ensure there is an 'x' in the check box in front of the label 'Label2'
1/3 - step 1 of in total 3 steps
$ - prompt for a regular user
# - prompt for the root user (system administrator)
host $ - prompt for the regular user on the host system
host # - prompt for the root user on the host system 
container $ - prompt for the regular user in the container
container # - prompt for the root user in the container

3. First step

The first step is to make a decision on which pre-existing image to use as a base. To name a few:

alpine - a rudimentary linux image ubuntu - a somewhat more complex linux image nginx - a linux image with a web server

Now that you have a base image, you edit the Dockerfile and make the following entry:

FROM <image>:<version>

where <image> is image you selected and <version> is the version of that image you want to use.

You could use as your version, which always point to the newest release of your image. Be aware, that this version might include incompatible changes to your environment when a new version is released.

You can see what versions of your image are available with the following command:

Example:

FROM ubuntu:18.04

You could already create an image with the Dockerfile just containing this line. When you start up this image, the container immediately ends, because you did not tell it what to do. Also, this image does not differ from the image you are basing this new image on.

To make the image do something useful, the first approach is to give you access to it. This can be done with the line:

CMD <program>

where <program> is a non-terminating executable. Should this end, so would the container. To peek into the container, it is useful to use a shell. By default, a Linux image includes /bin/sh. Other shells like ash, bash, ksh might or might not be included with all Linux images.

Example:

CMD /bin/sh

With the FROM line and the CMD line you now have the bare essential to start creating your own image. Your Dockerfile should now look like:

FROM ubuntu:18.04 CMD /bin/sh

4. Build your image

docs.docker.com

mkimage.sh:

TAG=latest
IMAGE=image_name
CONTAINER_NAME=container_name

sudo docker image build -t $IMAGE:$TAG .

Execute the mkimage.sh script inside the directory where the Dockerfile is.

host $ ./mkimage.sh

5. Start your image

start_image.sh:

TAG=latest
IMAGE=image_name
CONTAINER_NAME=container_name

# Activate the port settings, if you need to make a port from the container available on the host system
# If you do not need a port, you can also skip the '--publish' in the 'docker run' line below.
HOST_PORT=8080
CONTAINER_PORT=8080

sudo docker run --detach --publish=$HOST_PORT:$CONTAINER_PORT --name=$CONTAINER_NAME $IMAGE

sudo docker container ls -a | grep $CONTAINER_NAME

Execute the start_image.sh script

host $ start_image.sh

Note: You could change the 'docker run' line in the image not to include the '--detach' to gain access to the container after running the start_image.sh script. Yet, the ultimate goal is to create a container, who runs the application you need without having an interactive shell.

6. Access the container

host $ sudo docker exec -it $CONTAINER_NAME /bin/sh

Check what is missing in the image. Execute the command, make sure, it works, and add an appropriate line into the Dockerfile for it. For example:

Description: Update the list of available packages (no installation) Command: apt update Dockerfile entry: RUN apt update

Descritption: Upgrade newer available package Command: apt --yes upgrade Dockerfile entry: RUN apt --yes upgrade

Description: Install the fortune cookie module Command: apt install --yes fortune-mod Dockerfile entry: RUN apt install --yes fortune-mod Test: /usr/games/fortune

7. ...

8. Check the web server

host $ curl http://localhost:8080

9. docker-compose

In short, docker-compose is a configuration file to execute 'docker run' commands. 

default file name is docker-file.yml

$ sudo docker-compose [-f \<yml-file>] build

$ sudo docker-compose [-f \<yml-file>] up
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment