Kim Stacks
Kim Stacks

Reputation: 10822

How to test the container or image after docker build?

I have the following Dockerfile

############################################################
# Purpose   : Dockerize Django App to be used in AWS EC2
# Django    : 1.8.1
# OS        : Ubuntu 14.04
# WebServer : nginx
# Database  : Postgres inside RDS
# Python    : 2.7
# VERSION   : 0.1
############################################################

from ubuntu:14.04

maintainer Kim Stacks, [email protected]

# make sure package repository is up to date
run echo "deb http://archive.ubuntu.com/ubuntu $(lsb_release -sc) main universe" > /etc/apt/sources.list

run apt-get update

# install python

# install nginx

Inside my VM, I did the following:

docker build -t ubuntu1404/djangoapp .

It is successful.

What do I do to run the docker image? Where is the image or container?

I have already tried running

docker run ubuntu1404/djangoapp

Nothing happens.

What I see when I run docker images

root@vagrant-ubuntu-trusty-64:/var/virtual/Apps/DockerFiles/Django27InUbuntu# docker images
REPOSITORY             TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu1404/djangoapp   latest              cfb161605c8e        10 minutes ago      198.3 MB
ubuntu                 14.04               07f8e8c5e660        10 days ago         188.3 MB
hello-world            latest              91c95931e552        3 weeks ago         910 B

When I run docker ps, nothing shows up

Upvotes: 41

Views: 73680

Answers (3)

Sergej Brazdeikis
Sergej Brazdeikis

Reputation: 1373

How to test the container or image after docker build?

In order to test you can add write a bash script which will do the job https://blog.brazdeikis.io/posts/docker-image-tests

Btw, from the post, I see that it does not match the question from the title.

So, Added a link for the souls who arrived here based on the title...

Upvotes: 8

lazylead
lazylead

Reputation: 1989

  1. Download the latest shaded dist from https://github.com/dgroup/docker-unittests/releases:

    wget https://github.com/dgroup/docker-unittests/releases/download/s1.1.1/docker-unittests-app-1.1.1.jar
    
    
  2. De fine an *.yml file with tests.

    
    version: 1.1
    
    setup:
    - apt-get update
    - apt-get install -y tree
    
    tests:
    
    - assume: java version is 1.9, Debian build
     cmd:    java -version
     output:
       contains:
        - openjdk version "9.0.1"
        - build 9.0.1+11-Debian
    
    - assume: curl version is 7.xxx
     cmd:    curl --version
     output:
       startsWith: curl 7.
       matches:
        - "^curl\\s7.*\\n.*\\nProtocols.+ftps.+https.+telnet.*\\n.*\\n$"
       contains:
        - AsynchDNS IDN IPv6 Largefile GSS-API
    
    - assume:  Setup section installed `tree`
     cmd:     tree --version
     output:
       contains: ["Steve Baker", "Florian Sesser"]
    
    
  3. Run tests for image
    java -jar docker-unittests.jar -f image-tests.yml -i openjdk:9.0.1-11
    
    https://i.sstatic.net/DSv72.png

Upvotes: 0

vmonteco
vmonteco

Reputation: 15423

You have to give a command your container will have to process.

Example : sh

you could try :

docker run -ti yourimage sh

(-ti is used to keep a terminal open)

If you want to launch a daemon (like a server), you will have to enter something like :

docker run -d yourimage daemontolaunch

Use docker help run for more options.

You also can set a default behaviour with CMD instruction in your Dockerfile so you won't have to give this command to your container each time you want to run it.

EDIT - about container removing :

Containers and images are different. A container is an instance of an image. You can run several containers from the same image.

The container automatically stops when the process it runs terminates. But the container isn't deleted (just stopped, so you can restart it). But if you want to remove it (removing a container doesn't remove the image) you have two ways to do :

  • automatically removing it at the end of the process by adding --rm option to docker run.

  • Manually removing it by using the docker rm command and giving it the container ID or its name (a container has to be stopped before being removed, use docker stop for this).

A usefull command :

Use docker ps to list containers. -q to display only the container IDs, -a to display even stopped containers.

More here.

EDIT 2:

This could also help you to discover docker if you didn't try it.

Upvotes: 26

Related Questions