curl -s -o Dockerfile https://raw.githubusercontent.com/ajacocks/Dockerfile_fedora-postgres/master/Dockerfile_fedora-postgres
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
curl -s -o Dockerfile https://raw.githubusercontent.com/ajacocks/Dockerfile_fedora-postgres/master/Dockerfile_fedora-postgres
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 0.0.0.0/0 md5" >> /var/lib/pgsql/data/pg_hba.conf RUN echo "listen_addresses='*'" >> /var/lib/pgsql/data/postgresql.conf EXPOSE 5432 CMD ["/bin/postgres", "-D", "/var/lib/pgsql/data", "-c", "config_file=/var/lib/pgsql/data/postgresql.conf"]
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 .
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
Now, we can open a shell inside of the running container, to poke around inside:
podman exec -it fpg psql
\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
And finally, we can close down our connection to the container:
\q
Domain | ||
Workshop | ||
Student ID |