The software described in this documentation is either no longer supported or is in extended support.
Oracle recommends that you upgrade to a current supported release.

4.4 Creating a Docker Image from a Dockerfile

You use the docker build command to create a Docker image from the definition contained in a Dockerfile.

The following example demonstrates how to build an image named mymod/httpd with the tag v2 based on the oraclelinux:6 image so that it can run an Apache HTTP server.

To create a Docker image from a Dockerfile:

  1. Make a directory where you can create the Dockerfile, for example:

    # mkdir -p /var/docker_projects/mymod/httpd
    Note

    You do not need to create the Dockerfile on the same system on which you want to deploy containers that you create from the image. The only requirement is that the Docker Engine can access the Dockerfile.

  2. In the new directory, create the Dockerfile, which is usually named Dockerfile. The following Dockerfile contents are specific to the example:

    # Dockerfile that modifies oraclelinux:6 to include an Apache HTTP server
    FROM oraclelinux:6
    MAINTAINER A N Other <another@example.com>
    RUN sed -i -e '/^\[main\]/aproxy=http://proxy.example.com:80' /etc/yum.conf
    RUN yum -y install httpd
    RUN echo "HTTP server running on guest" > /var/www/html/index.html
    EXPOSE 80
    ENTRYPOINT /usr/sbin/httpd -D FOREGROUND

    The # prefix in the first line indicates that the line is a comment. The remaining lines start with the following instruction keywords that define how Docker creates the image:

    ENTRYPOINT

    Specifies the command that a container created from the image always runs. In this example, the command is /usr/sbin/httpd -D FOREGROUND, which starts the HTTP server process.

    EXPOSE

    Defines that the specified port is available to service incoming requests. You can use the -p or -P options with docker run to map this port to another port on the host. Alternatively, you can use the --link option with docker run to allow another container to access the port over Docker's internal network (see Section 4.6, “Communicating Between Docker Containers”).

    FROM

    Defines the image that Docker uses as a basis for the new image.

    MAINTAINER

    Defines who is responsible for the Dockerfile.

    RUN

    Defines the commands that Docker runs to modify the new image. In the example, the RUN lines set up the web proxy, install the httpd package, and create a simple home page for the server.

    For more information about other instructions that you can use in a Dockerfile, see https://docs.docker.com/engine/reference/builder/.

  3. Use the docker build command to create the image:

    # docker build --tag="mymod/httpd:v2" /var/docker_projects/mymod/httpd/
    Sending build context to Docker daemon 2.048 kB
    Step 1 : FROM oraclelinux:6
     ---> 768a3d7b605a
    Step 2 : MAINTAINER A N Other <another@example.com>
     ---> Running in bc28c7b6babf
     ---> 0eb554ad6a7b
    Removing intermediate container bc28c7b6babf
    Step 3 : RUN sed -i -e '/^\[main\]/aproxy=http://proxy.example.com:80' /etc/yum.conf
     ---> Running in 4bd1305ab0fc
     ---> 328ff7b80dc0
    Removing intermediate container 4bd1305ab0fc 
    Step 4 : RUN yum -y install httpd
     ---> Running in 991bcace0d34
    Loaded plugins: security, ulninfo
    Setting up Install Process
    Resolving Dependencies
    --> Running transaction check
    ---> Package httpd.x86_64 0:2.2.15-53.0.1.el6 will be installed
    --> Processing Dependency: httpd-tools = 2.2.15-53.0.1.el6 for package: httpd-2.2.15-53.0.1.el6.x86_64
    .
    .
    .
    Installed:
      httpd.x86_64 0:2.2.15-53.0.1.el6                                              
    
    Dependency Installed:
      apr.x86_64 0:1.3.9-5.el6_2             apr-util.x86_64 0:1.3.9-3.el6_0.1     
      apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1 httpd-tools.x86_64 0:2.2.15-53.0.1.el6
      mailcap.noarch 0:2.1.31-2.el6         
    
    Complete!
     ---> 153b5fa6ba92
    Removing intermediate container 991bcace0d34
    Step 5 : RUN echo "HTTP server running on guest" > /var/www/html/index.html
     ---> Running in 0a9d54558627
     ---> 6409e3ae11e1
    Removing intermediate container 0a9d54558627
    Step 6 : EXPOSE 80
     ---> Running in da51ae63fbe0
     ---> d16342a6b776
    Removing intermediate container da51ae63fbe0
    Step 7 : ENTRYPOINT /usr/sbin/httpd -D FOREGROUND
     ---> Running in 221988caab1c
     ---> 6df513b1c3d1
    Removing intermediate container 221988caab1c
    Successfully built 6df513b1c3d1

Having built the image, you can test it by creating a container instance named newguest2:

[root@host ~]# docker run -d --name newguest2 -P mymod/httpd:v2
31b334b9933cfbec71d7bc4f723c352c8de842823505b6f11a08bf960e0398e7
Note

You do not need to specify /usr/sbin/httpd -D FOREGROUND as this command is now built into the container.

The -P option specifies that Docker should map the ports exposed by the guest to available ports in the range 49000 through 49900 on the host.

You can use docker inspect to return the host port that Docker maps to TCP port 80:

[root@host ~]# docker inspect --format='{{ .NetworkSettings.Ports }}' newguest2
map[80/tcp:[map[HostIp:0.0.0.0 HostPort:49153]]]

In this example, TCP port 80 in the guest is mapped to TCP port 49153 on the host.

You can view the web content served by the guest by pointing a browser at port 49153 on the host. If you access the content from a different system, you might need to allow incoming connections to the port on the host.

For example you can open the port by creating an iptables rule:

[root@host ~]# iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 49153 -j ACCEPT
[root@host ~]# service iptables save

You can also use curl to test that the server is working:

[root@host ~]# curl http://localhost:49153
HTTP server running on guest
[root@host ~]# ssh auser@10.0.0.23
auser@10.0.0.23's password: password
Last login: Fri Aug 29 13:48:58 2014 from 192.168.0.1
[auser@10.0.0.23 ~]$ curl 192.168.0.2:49153
HTTP server running on guest