Purchase Linux CDs / DVDs / Flash Drives at OSDisc.com

Welcome to Our Community

While Linux.org has been around for a while, we recently changed management and had to purge most of the content (including users). If you signed up before April 23rd, 2017 please sign up again. Thanks!

  1. Follow us on twitter - we shoot all of our new original content out as well as random messages. https://twitter.com/linuxdotorg
    Dismiss Notice

Creating a Docker Container

Discussion in 'Linux Server' started by Jarret B, Feb 26, 2019.

  1. Jarret B

    Jarret B Active Member

    Joined:
    May 22, 2017
    Messages:
    104
    Likes Received:
    49
    To create a Docker Container which executes or contains a specific program you must first create the Docker image.
    You can kind of associate an image with an ISO file. The container is when the image is running, such as loading an ISO file in Virtualbox and installing it.
    Let’s look at creating an image file so we can start a container which we created.

    Creating an Image

    Some containers will run and allow interaction from other processes. For example, we can have a container which is a database program. We can access the database from other programs on the same system or from network systems. We can run the container in interactive mode to allow you to run applications from within the container itself. Interactive mode can allow you to run internal tests if something seems to not work.
    In this example we will make an image which will require you to run it in interactive mode to work.
    To start you need to use a Base Image. The Base Image is an initial image which contains the basic starting point to add your other applications or services. Let’s look at four Base Images (there are very many Base Images).

    1. Alpine
    2. Ubuntu
    3. Fedora
    4. CentOS
    Each of these is a very minimal installation of the specified Linux distro. These may be the main images you start with when making your own. Other images are listed at https://hub.docker.com/search?q=&type=image.
    To create your image you need to choose the Base Image to start. For my example I am using the Ubuntu Base Image.
    Next we need to create the ‘dockerfile’ which includes all instructions to put the new image together. The proper way is to create a folder for each Docker image you are creating. You can create a folder named Docker and then place a sub-folder for each image. So let’s create a folder in your Home folder called Docker and then change into that folder. In the Docker folder we create another called Test and then change into that folder. Within the Test folder you need to create a ‘dockerfile’ to use for all of our instructions to build a new image. Use the following commands:

    Code:
    cd ~
    mkdir Docker
    cd Docker
    mkdir Test
    cd Test
    touch dockerfile
    Now that everything is created we can start with the instructions to place within the ‘dockerfile’. Open the ‘dockerfile’ using your favorite text editor and add the following lines:

    Code:
    FROM ubuntu
    RUN apt-get update
    RUN apt-get install bastet -y
    CMD [“/usr/games/bastet”]


    Let’s look over these statements one line at a time.
    The first line uses the command ‘FROM’. The line states that the Base Image we will use to create our new image is ‘ubuntu’. We are starting with a very minimal install of Ubuntu.

    NOTE: When the command executes it will search the local Docker cache for the Ubuntu image. If it is not found it will download it. If the image is found it will verify that the image is the most current version. If the local image is not the newest version then Docker will download the newest version to the cache.


    (Log in to hide this advertisement)


    Once the Base Image is downloaded the next line is executed to the Base Image. You can see that the second line updates the repository list for Ubuntu.
    The third line installs the ‘bastet’ program from the Ubuntu Repository. Be sure when performing the ‘RUN’ commands that they do not need user interaction. For ‘apt-get install’ use the ‘-y’ parameter to automatically install the designated application.
    The last line gives Docker the command to be run when the container is started. The command can be overridden when the container is started.
    Save the file as ‘dockerfile’ and close your editor.
    We now need to build the image from the list of commands we have specified. Make sure you are at a prompt in Terminal and in the Test folder. When the image is built we will give it a name. The name can be any name you can use for naming a file. You also need to use a DockerID. You could use any name, but if you plan on uploading your images to the Docker Hub you need to Register for a free account. The account name you use is your DockerID.
    The command syntax is as follows:

    Code:
    docker build -t <dockerID>/<image-name> .


    NOTE: Do not forget the last dot(.) on the line. It builds from the ‘dockerfile’ in the current folder.

    So for me the command would be:

    Code:
    docker build -t jarretbuse/test .


    The build should proceed and appear similar to Figure 1.

    Figure 01.jpg
    FIGURE 1

    In Figure 1 you can see there are four steps. These correspond to our four commands in the ‘dockerfile’.
    At the end of each step should be a ‘--→’ followed by an address. These addresses correspond to information stored in the cache. If you re-build the image and part of the build is still in cache then it will not be rerun. The cache file will be grabbed and used which can save a lot of build time.
    In Step 1 you can see that the Ubuntu image was downloaded.
    In Step 2 the command ‘apt-get update’ shows all of its output when the local list is updated from the repositories.
    Step 3 displays the installation of ‘bastet’ to the image.
    Finally, Step 4 sets the initial command to be executed.
    At the end is a successful build and an ID of ‘027bcc915587’. We gave a DockerID and Image-Name which we can use to start the container or we can use the ID.
    Now that the image is built we can start it with the command:

    Code:
    docker run [options] [dockerID/image-name][command]


    In this instance we could run on of the two commands:

    Code:
    docker run -it jarretbuse/test
    docker run -it  027bcc915587


    Of course on your system your DockerID (first command) as well as the ID (second command) will be different.

    NOTE: For containers which require interaction from the keyboard you need to use the parameter ‘-it’.

    If you leave out the ‘it’ parameter you cannot press buttons on the keyboard and get the proper response from the container as in our test container. The container we built is a command-line clone of Tetris. You need to use the keyboard to move the pieces.
    When done with the Tetris clone you can select the ‘Quit’ option or press CTRL+C to exit the container.
    This should get you a basic understanding of using Docker to make images and start containers. Practice creating different images and starting a container.
     

Share This Page