Log4j does not log into Docker

Asked

Viewed 101 times

1

I’m starting now with Docker and am doing some testing with a small application.

I can upload the application in Docker however, I have the log4j configured to generate the log files in a specific path, /var/log/teste. When I upload the application on my machine - I am using Cent OS 7 - the log is generated, when I give a docker build ., Jetty goes up without errors, but does not log.

Follow my Dockerfile:

FROM maven:3.3.1-jdk-7

COPY . /usr/src/app
WORKDIR /usr/src/app

RUN mkdir /var/log/teste
RUN chmod 777 -R /var/log/teste

RUN mvn clean install

EXPOSE 8080

RUN mvn jetty:run

Does anyone have any suggestions to help me?

  • docker build just create the image, are you creating/running a container? For example, if you make a docker ps, Can you see if there’s a container running? If not, do something like docker run <parametros> <nome-da-sua-imagem> to start a new container, if you already have one created, do docker start <nome-container>

1 answer

0

Logs are not created because there is no container spinning. build build an image and make it available, so you can use it to create containers. The application construction logs, the Jetty running on the build and everything else is just the command creating the image, ie downloading the dependencies (as specified in RUN of Dockerfile, etc.) and not necessarily running the container. Note that until then we do not have one container, since build is an image command, not container. See us topics what is of one and another.

To run the entire stream you want, let’s consider that you have built your image as follows (assuming you are in the directory in which the Dockerfile):

docker build -t jetty-log4j .

If the construction is carried out successfully, by listing the images (docker images you’ll have something like this:

$ docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
jetty-log4j               <none>              81a458346fd6        5 minutes ago       605 MB

Created the image now we have to create a container and spin it and for this we have the run. This guy first creates a container and then starts it. It is the combination of create and start, just to make our lives easier :)

So, to create and start our container we can do it this way:

docker run --name jetty-log4j-test \
    -d \
    -p 8080:8080 \
    jetty-log4j

This will create a container called jetty-log4j and run it. It is at this time that your log will be created, as there is now in fact a container running, so your application is running.

If you want to execute the create and then the start, can do so:

docker create --name jetty-log4j-test \
    -d \
    -p 8080:8080 \
    jetty-log4j

And then:

docker start jetty-log4j-test

The start can be used from now on, no need to use the run again, since you already have the container created, only if you want another container of the same image.

Now we can do the docker ps to see if the container it’s really spinning and we’ll have something like this:

$ docker ps
CONTAINER ID        IMAGE                        COMMAND                CREATED              STATUS              PORTS               NAMES
9c01fb0b739a        jetty-log4j                  bash                   17 seconds ago       Up 16 seconds       8080/tcp            jetty-log4j-test

It is after this step that we can notice the log on path configured, since it is now that we actually have a container running, not just an image.

To stop the container just do this:

docker stop jetty-log4j-test

For other commands, see in the documentation everything available on cli.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.