Exercise 1.3 - Working with the Dockerfile

Return to Workshop

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 here involves creating a Dockerfile file 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

First get the Dockerfile from GitHub using curl.

Get file
curl -s -o Dockerfile https://raw.githubusercontent.com/RedHatGov/Dockerfile_fedora-postgres/master/Dockerfile_fedora-postgres
cat Dockerfile

FROM fedora
MAINTAINER jcallawa@redhat.com
RUN dnf 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
VOLUME	["/var/lib/pgsql/data"]
CMD ["/bin/postgres", "-D", "/var/lib/pgsql/data", "-c", "config_file=/var/lib/pgsql/data/postgresql.conf"]
mkdir build; mv Dockerfile build/Dockerfile
cd build; docker build -t fedora_postgresql .
docker run -d --name fpg fedora_postgresql
docker exec -it fpg psql
Note: This is backslash followed by lower-case L

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
(4 rows)
To exit

Return to Workshop