Khanetor
Khanetor

Reputation: 12304

Cannot start a docker container

This is my Dockerfile, which attempts to setup Nginx with Phusion Passenger, and then install Ruby.

# Build command: docker build --force-rm --tag="rails" .
# Run command: docker run -P -d rails
FROM ubuntu:14.04

ENV DEBIAN_FRONTEND noninteractive

# Install nginx and passenger
RUN sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 561F9B9CAC40B2F7
RUN sudo sudo apt-get install -yf apt-transport-https ca-certificates
RUN sudo echo 'deb https://oss-binaries.phusionpassenger.com/apt/passenger trusty main' > /etc/apt/sources.list.d/passenger.list
RUN sudo chown root: /etc/apt/sources.list.d/passenger.list
RUN sudo chmod 600 /etc/apt/sources.list.d/passenger.list
RUN sudo apt-get update
RUN sudo apt-get install -yf nginx-extras passenger
RUN sudo service nginx restart

# Install rbenv and ruby
RUN sudo apt-get install -yf git autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm3 libgdbm-dev curl
RUN git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
RUN git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
RUN echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
RUN echo 'eval "$(rbenv init -)"' >> ~/.bashrc
RUN /root/.rbenv/plugins/ruby-build/install.sh
ENV PATH /root/.rbenv/bin:$PATH
RUN rbenv install 2.1.5
RUN rbenv global 2.1.5

EXPOSE 80

Basically, I can build the image just fine, but I cannot start a container from this image.

This is my first time using docker, so I suspect that I need to use the CMD directive, but I have no idea what command should go there.

I appreciate any help to point out what is wrong with my Dockerfile.

Upvotes: 2

Views: 8709

Answers (3)

Adrian Mouat
Adrian Mouat

Reputation: 46470

The container is running successfully, its just exiting immediately as you don't specify any process to run. A container will only run as long as its main process. As you've run it in the background (the -d flag) it won't provide any output, which is a bit confusing.

For example:

$ docker run ubuntu echo "Hello World"
Hello World

The container ran the command and exited as expected.

$ docker run -d ubuntu echo "Hello World"
efd8f9980c1c9489f72a576575cf57ec3c2961e312b981ad13a2118914732036

The same thing as happened, but as we ran with -d, we got the id of the container back rather than the output. We can get the output using the logs command:

$ docker logs efd8f9980c1c9489f72a576575cf57ec3c2961e312b981ad13a2118914732036
Hello World

What you need to do is start your rails app, or whatever process you want the container to run when you launch the container. You can either do this from the docker run command or using CMD statement in the Dockerfile. Note that the main process must stay in the foreground - if it forks to the background the container will exit.

If you want to get a shell in a container, start it with -it e.g:

$ docker run -it ubuntu /bin/bash

To be honest, I think you'd be better served by using an official image e.g. https://registry.hub.docker.com/_/rails/.

Upvotes: 5

bachr
bachr

Reputation: 6006

You need to use ENTRYPOINT to start what you want, check the official documentation.

Upvotes: 2

user2915097
user2915097

Reputation: 32156

See what the logs says by issuing

docker logs rails

or, as

docker ps -an 1

should show the last id, with the command

docker logs this_id

Upvotes: 0

Related Questions