Exercise 1.3 - Working with the Dockerfile

Return to Workshop

Exercise 1.3 - Working with the Dockerfile

Exercise Description

This exercise will enable you to use a Dockerfile to build a container. Once you understand how images and containers can be created from the command line, you can try building containers in a more permanent way.

Building container images from Dockerfile files is, by far, the preferred way to create docker-formatted containers, as compared to modifying running containers and committing them to images.

The procedure involves creating a file in Dockerfile-format that includes many of the features illustrated earlier:

  • Choosing a base image (i.e. Fedora)

  • Installing the packages needed (i.e. an Postgresql Server)

  • Mapping the server’s port (i.e. 5432)

  • Launching the Database server

Step 1. Get the Dockerfile from GitHub using cURL:

curl -s -o Dockerfile https://raw.githubusercontent.com/ajacocks/Dockerfile_fedora-postgres/master/Dockerfile_fedora-postgres

Step 2. Review the container contents.

After downloading the Dockerfile, using the cURL command, review the contents of the container description, to gain an understanding of how containers are built:

cat Dockerfile
FROM docker.io/library/fedora:latest
MAINTAINER alexander@redhat.com
RUN yum install -y postgresql-server
USER postgres
RUN /bin/initdb -D /var/lib/pgsql/data
RUN /usr/bin/pg_ctl start -D /var/lib/pgsql/data -s -o "-p 5432" -w -t 300 &&\
                /bin/psql --command "CREATE USER docker WITH SUPERUSER PASSWORD 'docker';" &&\
                /bin/createdb -O docker docker
RUN echo "host all  all  md5" >> /var/lib/pgsql/data/pg_hba.conf
RUN echo "listen_addresses='*'" >> /var/lib/pgsql/data/postgresql.conf
CMD ["/bin/postgres", "-D", "/var/lib/pgsql/data", "-c", "config_file=/var/lib/pgsql/data/postgresql.conf"]

Step 3. Use buildah to build a container

Next, we will build the container, and store it in the local container image repository. Use the buildah command to create the container image using the following options:

  • The bud option to specify that we will build from a dockerfile format description

  • The -t fedora_postgresql specifying the tag, and

  • The . telling buildah to use the current directory as the build source:

mkdir build; mv Dockerfile build/Dockerfile; cd build
buildah bud -t fedora_postgresql .

Step 4. Run the container

Next, we will run the container. However, we are introducing a new flag to our use of podman. The -d flag tells podman to disconnect from the container, once it is executed, and to leave it running, while --name fpg gives it a name to reference:

podman run -d --name fpg fedora_postgresql

Step 5. Examine the container contents

Now, we can open a shell inside of the running container, to poke around inside:

podman exec -it fpg psql

Step 6. Let’s do a quick check to see what databases are available:

List of databases
Name    |  Owner   | Encoding  | Collate | Ctype |   Access privileges
docker    | docker   | SQL_ASCII | C       | C     |
postgres  | postgres | SQL_ASCII | C       | C     |
template0 | postgres | SQL_ASCII | C       | C     | =c/postgres          +
          |          |           |         |       | postgres=CTc/postgres
template1 | postgres | SQL_ASCII | C       | C     | =c/postgres          +
          |          |           |         |       | postgres=CTc/postgres

Step 7. Close the container connection.

And finally, we can close down our connection to the container:


Workshop Details

Domain Red Hat Logo
Student ID

Return to Workshop