• Installation (sudo required)
  • GIT
  • Dockerfile
  • Docker build and run
  • Installation (sudo required)
  • GIT
  • Dockerfile
  • Docker build and run
  • Getting started with Docker

    So you have docker installed (using my installer hopefully?), and you want to know how to begin.

    This should get you started in creating your first Docker instance.

    First off, let's install Docker.
    Previously, I had posted my Docker installers, so we are going to use those for this exercise. I have broken out the instructions for both Mac and Debian-based when needed.

    Installation (sudo required)

    Mac

    curl -s bman.io/i/install_dockermac|sudo bash  
    

    or

    curl -s https://gist.githubusercontent.com/bhgraham/9dced0918dc20edd1484/raw/744e2f3ec355dc9a9310527c77234dbb2a192cde/install_dockermac | sudo bash  
    

    Debian-based

    curl -s bman.io/i/install_dockerdeb|sudo bash  
    

    or

    curl -s https://gist.githubusercontent.com/bhgraham/ed9f8242dc610b1f38e5/raw/58c162147be40c53a8a35b525e62dea86f49ebec/install_dockerdeb | sudo bash  
    

    GIT

    To make use of common best practices, we are going to need to use revision control. We will need a directory to initialize a GIT repository. For our exercise we are going to use the project name, exampledocker.

    mkdir exampledocker && cd exampledocker  
    git init  
    

    Dockerfile

    Using your preferred editor (nano, vim, eg.), you need to create a file named Dockerfile with the following contents:

    FROM    debian:stable
    
    MAINTAINER MyName <me@wherever.com>
    
    # Build dependencies
    RUN apt-get -y update
    
    # Install some common tools needed.
    RUN apt-get install -y -q curl git-core apt-utils sudo libwww-perl vim htop wget
    
    # Setup timezone, notice in this example, we 
    # perform multiple operations within the same
    # RUN by ending the lines with \, this ensures
    # You create a single build step for this operation.
    RUN \  
      cp /usr/share/zoneinfo/America/Chicago /etc/localtime && \
      echo "America/Chicago" > /etc/timezone;
    
    # Bash / sh link
    RUN ln -sf /bin/bash /bin/sh;
    
    # Say you want a file with predefines set,
    # like for a file in /etc, you can do it like so.
    RUN echo moo > /tmp/moo
    
    # When the container is run, which directory do 
    # you want it dropped into?
    WORKDIR  /opt/
    
    # Now we are going to set the command executed when 
    # the container is run.
    CMD ["/bin/bash"]  
    

    Save that file and then add it to git.

    git add Dockerfile  
    git commit -m "Initial Dockerfile commit"  
    

    Docker build and run

    Now we will test out our Dockerfile by building it with the following command.

    docker build -t exampledocker .  
    

    Next you will see a ton of output showing whats going on. It begins like so, but I won't show the guts of it.

    Sending build context to Docker daemon  2.56 kB  
    Sending build context to Docker daemon  
    Step 0 : FROM debian:stable  
    debian:stable: The image you are pulling has been verified  
    798202714a7c: Downloading 78.93 MB/90.17 MB 7s  
    

    ...

    Removing intermediate container 66a45bcf45ac  
    Step 8 : CMD /bin/bash  
     ---> Running in aa69d44c9867
     ---> e885d8e03847
    Removing intermediate container aa69d44c9867  
    Successfully built e885d8e03847  
    

    Now that it is built, let's check to be sure it exists with the docker images command.

    docker images exampledocker  
    
    REPOSITORY     TAG     IMAGE ID      CREATED         VIRTUAL SIZE  
    exampledocker  latest  e885d8e03847  6 minutes ago  203.5 MB
    

    As we have set the command and the directory at the end of the Dockerfile, running the next command should drop you into a bash shell in /opt.

    docker run -i -t --rm exampledocker  
    

    The -i is interactive, -t allocates a pseudo-tty and --rm removes the container when you exit. I would recommend using --rm while you are still developing or else it gets messy fast.

    The result should look like this:

    root@1d14c22b52ab:/opt#  
    

    At this point you have successfully created and run your first container using Docker. Just type exit to quit the container and remove it.

    Check to be sure the container ended with the docker ps command. Just look for Exited (0)

    docker ps -a | grep dockerexample  
    

    That's it. You have now successfully created and run a docker container on your local machine.

    I will be writing a future article and going in depth on collaboration with Docker and using the Docker Hub. Having it in GIT from the start helps. As always, if you have any questions or comments, drop them on the post comments.

    If you need any other help you may want to use man or check here:
    https://docs.docker.com/reference/commandline/cli/

    bmanio forum