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.3 Creating a Docker Image from an Existing Container

If you modify the contents of a container, you can use the docker commit command to save the current state of the container as an image.

The following example demonstrates how to modify an container based on the oraclelinux:6.6 image so that it can run an Apache HTTP server. After stopping the container, the image mymod/httpd:v1 is created from it.

To create an Apache server image from an oraclelinux:6.6 container:

  1. Run the bash shell inside a container named guest:

    [root@host ~]# docker run -i -t --name guest oraclelinux:6.6 /bin/bash
    [root@guest ~]#
  2. If you use a web proxy, edit the yum configuration on the guest as described in https://docs.oracle.com/cd/E52668_01/E54669/html/ol7-proxy-config.html in the Oracle Linux Administrator's Guide for Release 7.

  3. Install the httpd package:

    [root@guest ~]# yum install httpd
  4. If required, create the web content to be displayed under the /var/www/html directory hierarchy on the guest.

  5. Exit the guest by using the docker stop command on the host:

    [root@host ~]# docker stop guest
    guest
  6. Create the image mymod/httpd with the tag v1 using the ID of the container that you stopped:

    [root@host ~]# docker commit -m "ol6 + httpd" -a "A N Other" \
      `docker ps -l -q` mymod/httpd:v1
    8594abec905e6374db51bed1bfb208804cfb60d96b285efb897db581a01676e9

    Use the -m and -a options to document the image and its author. The command returns the full version of the new image's ID.

    If you use the docker images command, the new image now appears in the list:

    [root@host ~]# docker images
    REPOSITORY    TAG         IMAGE ID       CREATED       VIRTUAL SIZE
    mymod/httpd   v1          8594abec905e   2 minutes ago 938.5 MB
    oraclelinux   6           9ac13076d2b5   5 days ago    319.4 MB
    oraclelinux   6.6         9ac13076d2b5   5 days ago    319.4 MB
    oraclelinux   latest      073ded22ac0f   5 days ago    265.2 MB
    oraclelinux   7           073ded22ac0f   5 days ago    265.2 MB
    oraclelinux   7.0         073ded22ac0f   5 days ago    265.2 MB
  7. Remove the container named guest.

    # docker rm guest
    guest

You can now use the new image to create a container that works as a web server, for example:

# docker run -d --name newguest -p 8080:80 mymod/httpd:v1 /usr/sbin/httpd -D FOREGROUND
7afbbefec5191f632e149f85ae10ed0ba88f1c545daad18cb930e575ef6a3e63

The -d option runs the command non-interactively in the background and displays the full version of the unique container ID. The -p 8080:80 option maps port 80 in the guest to port 8080 on the host. You can view the port mapping by running docker ps or docker port, for example:

[root@host ~]# docker ps
CONTAINER ID  IMAGE         COMMAND    CREATED    STATUS   PORTS                  NAMES
7afbbefec519  mymod/httpd:v1           ...        ...      0.0.0.0:8080->80/tcp   newguest
[root@host ~]# docker port newguest 80
0.0.0.0:8080
Note

The docker ps command displays the short version of the container ID. You can use the --no-trunc option to display the long version.

The default IP address value of 0.0.0.0 means that the port mapping applies to all network interfaces on the host. You can restrict the IP addresses to which the remapping applies by using multiple -p options, for example:

# docker run -d --name newguest -p 127.0.0.1:8080:80 -p 192.168.1.2:8080:80 \
  mymod/httpd:v1 /usr/sbin/httpd -D FOREGROUND

You can view the web content served by the guest by pointing a browser at port 8080 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:

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

If you need to remove an image, use the docker rmi command:

[root@host ~]# docker rmi mymod/httpd:v1
Untagged: mymod/httpd:v1
Deleted: 7afbbefec5191f632e149f85ae10ed0ba88f1c545daad18cb930e575ef6a3e63
Note

From version 1.8 of Docker, you cannot remove the image of a running container.

In a production environment, using the docker commit command to create an image does not provide a convenient record of how you created the image so you might find it difficult to recreate an image that has been lost or become corrupted. The preferred method for creating an image is to set up a Dockerfile, in which you define instructions that allow Docker to build the image for you. See Section 4.4, “Creating a Docker Image from a Dockerfile”.